<?php
/**
 * ApiBase API模块下公共控制器，需要登录的需要继承此控制器
 * Created by Veyron
 * User: Vy
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/1/17
 * Time: 11:33
 */
namespace app\api\controller;

class AuthBase extends  ApiBase {
    public $accessToken = '';
    public $userId = 0;
    public $username = '';
    public $isLogo = 1; //用于api测试，1时测试接口不需要登录，正式设置为0
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->accessToken = $this->request->header('access-token');
        if($this->isLogo == 1){
            $this->userId = '2'; //测试id
            return true;
        }
        if(!$this->accessToken || !$this->isLogin()){
            return $this->show(config('status.not_login'),'未登录');
        }
    }

    /**
     * 判断是否登录
     */
    public function isLogin(){
        $userInfo = cache(config('redis.token_pre').$this->accessToken);
        if(!$userInfo){
            return false;
        }
        if(!empty($userInfo['id']) && !empty($userInfo['username'])){
            $this->userId = $userInfo['id'];
            $this->username = $userInfo['username'];
            return true;
        }
        return false;
    }
}
